package bTree.LeetCode;

public class 最长回文字串 {
    public String longestPalindrome(String s) {
        int length = s.length();
        boolean[][] dp = new boolean[length + 1][length + 1];
        String result = "";
        int maxLength = 0;
        for (int i = length; i > 0; i--) {
            for (int j = i; j <= length; j++) {
                if (s.charAt(i - 1) == s.charAt(j - 1)) {
                    if (j - i <= 1) {
                        dp[i][j] = true;
                        if (j - i + 1 > maxLength) {
                            maxLength = j - i + 1;
                            result = s.substring(i, j + 1);
                        }
                    } else {
                        if (dp[i + 1][j - 1]) {
                            dp[i][j] = true;
                            if (j - i + 1 > maxLength) {
                                maxLength = j - i + 1;
                                result = s.substring(i, j + 1);
                            }
                        }
                    }
                }
            }
        }
        return result;
    }
}